/**
  ******************************************************************************
  * @file    zqfifo.c 
  * @author  Aizbot
  * @brief   xxx.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2023 Aizbot.
  * All rights reserved.</center></h2>
  *
  * http://www.aizbot.com
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "zqfifo.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
  * @brief  xxx.
  * @param  None
  * @retval None
  */
void Zqfifo_Init(Zqfifo_t *fifo, uint8_t *buf, uint16_t size)
{
	fifo->size = size;
	fifo->read = 0;
	fifo->write = 0;
	fifo->buffer = buf;
}

/**
  * @brief  xxx.
  * @param  None
  * @retval None
  */
void Zqfifo_Write(Zqfifo_t *fifo, uint8_t data)
{
	fifo->buffer[fifo->write++] = data;
	if(fifo->write >=  fifo->size )
	{
		fifo->write = 0;
	}
}

/**
  * @brief  xxx.
  * @param  None
  * @retval None
  */
uint8_t Zqfifo_Read(Zqfifo_t *fifo)
{
	uint8_t data = 0;
	
	data = fifo->buffer[fifo->read++];
	if(fifo->read >=  fifo->size )
	{
		fifo->read = 0;
	}
	return data;
}

/**
  * @brief  xxx.
  * @param  None
  * @retval None
  */
uint16_t Zqfifo_Count(Zqfifo_t *fifo)
{
	int16_t temp = 0;

	temp = fifo->write - fifo->read;
	if(fifo->write < fifo->read)
	{
		temp = temp + fifo->size;
	}
	return temp;
}

/**
  * @brief  xxx.
  * @param  None
  * @retval None
  */
uint8_t Zqfifo_GetCurrentValue(Zqfifo_t *fifo)
{
	uint8_t data = 0;
	
	data = fifo->buffer[fifo->write - 1];
	return data;
}

/**
  * @brief  xxx.
  * @param  None
  * @retval None
  */
uint8_t Zqfifo_GetPreviousValue(Zqfifo_t *fifo)
{
	uint8_t data = 0;
	
	data = fifo->buffer[--fifo->write];
	return data;
}

/************************ (C) COPYRIGHT Aizbot ********************************/
